package org.test.textToSpeak.service;

import cn.hutool.core.io.FileUtil;
import com.jacob.activeX.ActiveXComponent;
import com.jacob.com.Dispatch;
import com.jacob.com.LibraryLoader;
import com.jacob.com.Variant;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.stereotype.Service;


/**
 * @authorLHB
 * @date2019/10/2315:06
 */
@Service
public class SpeakService implements ApplicationRunner {
    static {
        LibraryLoader.loadJacobLibrary();
    }

    public void test() {
        ActiveXComponent sap = new ActiveXComponent("Sapi.SpVoice");

        Dispatch sapo = sap.getObject();
        String textContext = FileUtil.readUtf8String("G:\\mySpringCloud\\textToSpreak\\src\\main\\resources\\test.text");
        try {
            //音量0-100
            sap.setProperty("Volume", new Variant(100));
            //语音朗读速度-10到+10
            sap.setProperty("Rate", new Variant(2));
            //执行朗读
            // Dispatch.invoke("", "SaveAs", Dispatch.Method, new int[1]);
            //Dispatch.call(sapo, "Speak", new Variant("java,是世界上最好的语言！"));
            Dispatch.call(sapo, "Speak", new Variant(textContext));
        } catch (Exception exception) {

        } finally {
            sapo.safeRelease();
            sap.safeRelease();
        }
    }


    @Override
    public void run(ApplicationArguments args) {
        test();
    }
}
